package com.hhd.flow.support;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileNameUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.PassTaskContext;
import com.hhd.flow.engine.support.*;
import com.hhd.flow.engine.utils.CommonUtils;
import com.hhd.flow.engine.vo.FlowUser;
import com.hhd.flow.engine.vo.FlowUserProperty;
import com.hhd.flow.engine.vo.UserTask;
import com.hhd.flow.entity.FlowAttachment;
import groovy.lang.Lazy;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class DefaultFlowEngineSupport implements FlowEngineSupport {
    String basePath = "C:/upload";

    @Override
    public List<FlowUser> loadFlowUser(String formTypeId, String formId, UserTask userTask, PassTaskContext context) {
        String taskName = userTask.getTaskName();
        if ("2".equals(taskName)) {
            return CommonUtils.asList(new FlowUser("tangzy", "唐泽宇"), new FlowUser("tangzy1", "唐泽宇"), new FlowUser("tangzy2", "唐泽宇"));
        }
        if ("1".equals(taskName)) {
            return CommonUtils.asList(new FlowUser("tangzy", "唐泽宇"));
        }
        if ("4".equals(taskName)) {
            userTask.setManualSelectUser(true);
            userTask.setUserMenuSelectReason(userTask.getTaskName() + "节点找不到审批人，请选择审批人");
            return getAllUsers();
        }
        if ("5".equals(taskName)) {
//            userTask.setManualSelectUser(true);
//            userTask.setUserMenuSelectReason(userTask.getTaskName() + "测试原因");
            return getAllUsers();
        }
        if ("3".equals(taskName) || "333".equals(taskName)) {
            userTask.setManualSelectUser(true);
            userTask.setUserMenuSelectReason(userTask.getTaskName() + "节点找不到审批人，请选择审批人");
            return getAllUsers();
        }
        return CommonUtils.asList(new FlowUser("tangzy", "唐泽宇"));
    }

    private List<FlowUser> getAllUsers() {
        List<FlowUser> res = CommonUtils.newList();
        for (int i = 0; i < 23; i++) {
            FlowUser flowUser = new FlowUser("test-" + i, "test-" + i);
            FlowUserProperty flowUserProperty = new FlowUserProperty();
            flowUserProperty.setTitle("职务级别");
            flowUserProperty.setValue("董事长" + i);
            flowUser.setProperties(CommonUtils.asList(flowUserProperty));
            res.add(flowUser);
        }
        return res;
    }

    @Override
    public FlowUser getCurrentUser() {
        return new FlowUser("tangzy", "唐泽宇");
    }

    @Override
    public FlowAttachment upload(FlowResource resource, AttachMentType attachMentType) {
        FlowAttachment attachment = new FlowAttachment();
        attachment.setFileName(resource.getName());
        try {
            attachment.setFileSize(resource.getInputStream().available());
            attachment.setFileSizeReadAble(FileUtil.readableFileSize(resource.getInputStream().available()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        attachment.setAttachmentType(attachMentType.name());
        String path = "/flow/" + getDatePath() + "/" + attachMentType.name() + "/" + IdWorker.get32UUID() + "." + FileNameUtil.extName(resource.getName());
        attachment.setFilePath(path);
        attachment.setExt(FileNameUtil.extName(resource.getName()));

        FileUtil.mkParentDirs(basePath + path);
        try (FileOutputStream os = new FileOutputStream(basePath + path);
             InputStream is = resource.getInputStream()) {
            IoUtil.copy(is, os);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return attachment;
    }

    @Override
    public FlowResource loadResource(FlowAttachment attachment) {
        DefaultFlowResource resource = new DefaultFlowResource();
        try {
            resource.setInputStream(new FileInputStream(basePath + attachment.getFilePath()));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        return resource;
    }

    @Override
    public FlowUser getFlowUserById(String userId) {
        FlowUser flowUser = new FlowUser();
        flowUser.setId("test");
        flowUser.setUsername("唐泽宇");
        flowUser.setAvatar(new ClassPathResource("avatar", "/avatar/defaut.png"));
        return flowUser;
    }

    @Override
    public String getCurrentUserSign() {
        return null;
    }

    private String getDatePath() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(new Date());
    }
}
